<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use App\Models\Picture;
class Category extends Model
{
    
    /**
     * 表名称
     * @var $table;
     */
    protected $table = 'category';

    /**
     * 主键
     * @var $primaryKey
     */
    protected $primaryKey = 'cat_id';

    /**
     * 时间戳   laravel ORM 自动维护数据库时间戳,用户数据结构的回滚操作,默认开启,我们禁止
     * @var bool $timestamps
     */
    public $timestamps = false;
    protected $appends = array('mobile_cat_picture');
    /**
     * 自动填充字段
     * @var $fillable
     */

    protected $fillable = ['cat_name', 'keywords', 'cat_desc', 'parent_id', 'sort_order', 'measure_unit', 'goods_type', 'tariff_percent','excise_duty_percent','value_added_tax_percent','is_recommend','cat_picture'];

    function getMobileCatPictureAttribute(){

        $cat_picture = $this->getAttribute('cat_picture');
        if(!$cat_picture){
            return null;
        }

        return Picture::find($cat_picture)->url;


    }
    public static function getTreeList(){
        $list = new Collection();

        $roots = self::rootNodes()->orderBy('sort_order','desc')->orderBy('cat_id','asc')->get();
        foreach ($roots as $item) {
            $list->push($item);

            $item->setAttribute('deepth',0);
            $item->setAttribute('has_children',$item->hasChildren());
            $list = $list->merge($item->getDescendantList(0));
        }
        return $list;

    }


    public function scopeRootNodes($query) {
        return $query->where('parent_id','');
    }//end func scopeRootNodes()


    public function hasChildren() {

        if ($this->getChildren()->count()>0) return true;

    }//end func hasChildren()

    public function getParent(){
        return self::where('cat_id',$this->getAttribute('parent_id'))->first();
    }



    public function getChildren() {
        return self::where('parent_id', $this->getAttribute('cat_id'))->orderBy('sort_order','desc')->orderBy('cat_id','asc')->get();
    }//end func getChildren)


    public static function getTree() {
        $roots = self::rootNodes()->orderBy('sort_order','desc')->orderBy('cat_id','asc')->where('is_recommend',1)->get();
        foreach ($roots as $item) {
            $item->setAttribute('children', $item->getDescendants());
        }
        return $roots;
    }

    public function getDescendants() {
        //$children = $this->getChildren();
        $children = self::where('parent_id', $this->getAttribute('cat_id'))->orderBy('sort_order','desc')->orderBy('cat_id','asc')->where('is_recommend',1)->get();

        foreach ($children as $item) {
            $item->setAttribute('children', $item->getDescendants());
        }
        return $children;
    }

    private function getDescendantList($deepth){

        $deepth++;

        $list = new Collection();

        $childCollection = $this->getChildren();

        foreach ($childCollection as $item) {
            $item->setAttribute('deepth',$deepth);
            $item->setAttribute('has_children',$item->hasChildren());

            $list->push($item);
            if ($item->hasChildren()) {
                $list = $list->merge($item->getDescendantList($deepth));
            }
        }
        return $list;
    }//end func getDescendantList()

    public static function getRootIds(){
        $ids = new Collection();
        $roots = self::rootNodes()->orderBy('sort_order','desc')->orderBy('cat_id','asc')->get();
        foreach ($roots as $k=>$child){
            $ids = $ids->merge($child->cat_id);
            $ids = $ids->merge($child->getDescendantIds());
        }
        return $ids;
    }


    public function getChildIds(){
        $ids = new Collection();
        $children = $this->getChildren();
        foreach ($children as $k=>$child){
            $ids = $ids->merge($child->cat_id);
            $ids = $ids->merge($child->getDescendantIds());
        }
        return $ids;

    }

    private function getDescendantIds(){


        $ids = new Collection();

        $childCollection = $this->getChildren();
        foreach ($childCollection as $k=>$child){
            $ids = $ids->merge($child->cat_id);
        }
        foreach ($childCollection as $item) {

            $ids->push($item->cat_id);
            if ($item->hasChildren()) {
                $ids = $ids->merge($item->getDescendantIds());
            }
        }
        return $ids;
    }//end func getDescendantList()

    public function getPath(){

        $path = [];

        $parentCat = $this->getParent();

        if ($parentCat){

            array_push($path,$parentCat);

            if ($parentCat->getParent()){
                $path = array_merge($path,$parentCat->getPath());
            }
        }

        return $path;
    }

    public function getDepth(){

        static $depth=1;

        $parentCat = $this->getParent();

        if ($parentCat){
            $depth++;

            if ($parentCat->getParent()){
                $parentCat->getDepth();
            }
        }
        return $depth;

    }



}//end calss Category
